﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeaturesUse
{
    public class DisposableClass
    {
        private readonly ref struct Polygon
        {
            public readonly Span<int> points = new int[100];
            public Polygon()
            {
                points.Fill(1);
            }
            public void Dispose()
            {
                points.Fill(0);
                Console.WriteLine("Polygon Disposed");
            }
        }
        /// <summary>
        /// Struct位于栈中，没有析构函数，随函数结束而结束
        /// Dispose使得Struct可以在生命周期结束时析构
        /// </summary>
        public static void ShowUse()
        {
            // 正常执行
            using (Polygon polygon1 = new Polygon())
            {
                polygon1.points[0] = 0;
                polygon1.points[1] = 1;
            }
            //当使用完毕，你会发现它执行了Dispose()

            // 不用using则不会执行Dispose()
            // Struct位于栈中，没有析构函数，随函数结束而结束
            Polygon polygon = new Polygon();
            polygon.points[0] = 0;
            polygon.points[1] = 1;
            
        }
    }
}